import { setLogin, setRegister, setSmsCode, getUserInfo } from './../../api/user/index';
import { IGlobalState } from './../index';
import { IUserState, ILoginForm, IregisterForm, IUserInfo } from './../../typeings/user';
import {Module} from 'vuex'
import * as Types from '../action-types'

const state:IUserState  ={
	loginForm:{
		username: '',
		password: '',
		uuid:""
	},
	isLogin:false,
	registerForm:{
		username: '',
		password: '',
		passwordsed:'',
		uuid:'',
		smsCode:'',
		smsSqe:''
	},
	userInfo:{
		userInfo:{
			nickName:"",
			ID:"",
			integration:""
		},
		userConfigList:[],
		userAccountList:[]
	}
}
const mutations = {
	//login
	[Types.SET_LOGIN](state:IUserState, paylpad:ILoginForm){
		state.loginForm = paylpad
		state.isLogin = true
	},
	//register
	[Types.SET_REGISTER](state:IUserState, paylpad:IregisterForm){
		state.registerForm = paylpad
	},
	//smsCode
	[Types.SET_SMSCODE](state:IUserState, paylpad:IregisterForm){
		let {smsCode,smsSqe,uuid} = paylpad
		state.registerForm.smsCode = smsCode
		state.registerForm.smsSqe = smsSqe
		state.registerForm.uuid = uuid
	},
	//userInfo
	[Types.GET_USERINFO](state:IUserState, paylpad:IUserInfo){
		state.userInfo = paylpad
		state.isLogin = true
	}
}

const actions = {
	async [Types.SET_LOGIN]({commit}:any,payload:ILoginForm){
		let login = await setLogin(payload);
		commit(Types.SET_LOGIN,login)
	},
	async [Types.SET_REGISTER]({commit}:any,payload:IregisterForm){
		let register = await setRegister(payload);
		commit(Types.SET_REGISTER,register)
	},
	async [Types.SET_SMSCODE]({commit}:any,payload:IregisterForm){
		let uuid =payload.uuid.value
		let result = await setSmsCode(payload);
		commit(Types.SET_SMSCODE,{...result,uuid})
	},
	async [Types.GET_USERINFO]({commit}:any){
		let result = await getUserInfo();
		commit(Types.GET_USERINFO,result)
	}
}
const user:Module<IUserState,IGlobalState> ={
	namespaced:true,
	state,
	mutations,
	actions
}
export default user;